package net.lab1024.sa.admin.module.business.picture.service;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.lab1024.sa.admin.module.business.picture.dao.PictureDao;
import net.lab1024.sa.admin.module.business.picture.domain.entity.PictureEntity;
import net.lab1024.sa.admin.module.business.picture.domain.form.PictureAddForm;
import net.lab1024.sa.admin.module.business.picture.domain.form.PictureQueryForm;
import net.lab1024.sa.admin.module.business.picture.domain.form.PictureUpdateForm;
import net.lab1024.sa.admin.module.business.picture.domain.vo.PictureVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;

/**
 * 全景图添加 Service
 *
 * @Author 骆伟林
 * @Date 2025-10-22 17:09:49
 * @Copyright 软件需求工程第八组
 */

@Service
public class PictureService {

    @Resource
    private PictureDao pictureDao;

    /**
     * 分页查询
     */
    public PageResult<PictureVO> queryPage(PictureQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<PictureVO> list = pictureDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> add(PictureAddForm addForm) {
        PictureEntity pictureEntity = SmartBeanUtil.copy(addForm, PictureEntity.class);
        pictureDao.insert(pictureEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量新增
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> batchAdd(List<PictureAddForm> addFormList) {
        List<PictureEntity> pictureEntityList = SmartBeanUtil.copyList(addFormList, PictureEntity.class);
        pictureDao.insert(pictureEntityList);
        return ResponseDTO.ok();
    }


    /**
     * 更新
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(PictureUpdateForm updateForm) {
        PictureEntity pictureEntity = SmartBeanUtil.copy(updateForm, PictureEntity.class);
        pictureDao.updateById(pictureEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }
        pictureDao.batchUpdateDeleted(idList, true);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long pitureId) {
        if (null == pitureId) {
            return ResponseDTO.ok();
        }

        pictureDao.updateDeleted(pitureId, true);
        return ResponseDTO.ok();
    }

    /**
     * 获取图片id和名称列表
     */
    public List<PictureVO> getPictureList() {
        return SmartBeanUtil.copyList(pictureDao.selectList(new LambdaQueryWrapper<PictureEntity>().select(PictureEntity::getPitureId, PictureEntity::getPictureName)), PictureVO.class);
    }

    /**
     * 根据地点id获取图片列表
     */
    public List<PictureVO> getPictureListByAddressId(Long addressId) {
        return SmartBeanUtil.copyList(pictureDao.selectList(new LambdaQueryWrapper<PictureEntity>().eq(PictureEntity::getAddressId, addressId)), PictureVO.class);
    }

    /**
     * 根据地点id批量删除图片列表
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> batchDeleteByAddressId(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }
        pictureDao.batchUpdateDeletedByAddressId(idList, true);
        return ResponseDTO.ok();
    }
    /**
     * 根据图片id获得详情
     */
    public PictureVO getPictureById(Long pitureId) {
        return SmartBeanUtil.copy(pictureDao.selectById(pitureId), PictureVO.class);
    }



}
